package com.earl.gsm.service.impl.security;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.earl.gsm.commons.dao.BaseDao;
import com.earl.gsm.commons.service.BaseServiceImpl;
import com.earl.gsm.dao.RoleDao;
import com.earl.gsm.dao.RolePrivilegeDao;
import com.earl.gsm.domain.sercurity.privilege.Privilege;
import com.earl.gsm.domain.sercurity.role.Role;
import com.earl.gsm.domain.sercurity.roleprivilege.RolePrivilege;
import com.earl.gsm.service.RoleService;
import com.earl.gsm.service.impl.match.HistoryMatchServiceImpl;

@Service
public class RoleServiceImpl extends BaseServiceImpl<Role,Role> implements RoleService{

	
	  private static Logger logger = Logger.getLogger(HistoryMatchServiceImpl.class);

	    @Resource
	    private RoleDao dao;

	    @Resource
	    private RolePrivilegeDao rolePrivilegeDao;

	    
	    @Override
	    protected BaseDao<Role> getDao() {
	        return dao;
	    }

		@Override
		public List<Privilege> findRolePrivilege(Long roleId) {
			// TODO Auto-generated method stub
			return dao.findRolePrivilege(roleId);
		}

		@Override
		public void addRoleInfo(Role role,Integer[] privilegeId){
			Integer roleId = dao.insertBackId(role);
			updatePrivilege(roleId.longValue(),privilegeId);
		}

		@Override
		public void updateRoleInfo(Role role, Integer[] privilegeId) {
			dao.updateByPrimaryKeySelective(role);
			updatePrivilege(role.getId(),privilegeId);
		}
		
		public void updatePrivilege(Long roleId,Integer[] privilegeId){
			
			RolePrivilege rpQuery = new RolePrivilege();
			rpQuery.setRoleId(roleId);
			rolePrivilegeDao.deleteByQuery(rpQuery);
			for (Integer integer : privilegeId) {
				RolePrivilege rp = new RolePrivilege();
				rp.setRoleId(roleId);
				rp.setPrivilegeId(integer.longValue());
				rolePrivilegeDao.insert(rp);
			}
		}
}